package com.apobates.jforum2.troll.accounts.mapper;

import com.apobates.jforum2.troll.accounts.entity.MemberOnline;
import com.apobates.jforum2.troll.accounts.stub.vo.MemberOnlineVo;
import com.apobates.jforum2.troll.regular.ForumActionEnum;
import com.apobates.jforum2.troll.utils.core.api.EntityVoTypeAdapter;
import com.apobates.jforum2.troll.utils.core.api.OptionalEntityTransformMapper;

import java.util.Optional;

/**
 *
 * @author xiaofanku
 */
public class MemberOnlineVoTypeAdapter implements EntityVoTypeAdapter<MemberOnline, MemberOnlineVo>, OptionalEntityTransformMapper<MemberOnline, MemberOnlineVo> {
    @Override
    public MemberOnlineVo marshal(MemberOnline entity) {
        MemberOnlineVo mo = new MemberOnlineVo();
        mo.setSessionId(entity.getId());
        mo.setMember(entity.getMid());
        mo.setNames(entity.getMemberNames());
        mo.setNickname(entity.getMemberNickname());
        mo.setEntryDateTime(entity.getActiveDateTime());
        mo.setAction(entity.getAction().getSymbol());
        return mo;
    }

    @Override
    public MemberOnline unmarshal(MemberOnlineVo target) {
        MemberOnline m = new MemberOnline();
        m.setAction(ForumActionEnum.getInstance(target.getAction()).orElse(ForumActionEnum.COMMON_NUE));
        m.setActiveDateTime(target.getEntryDateTime());
        m.setMemberNickname(target.getNickname());
        m.setMemberNames(target.getNames());
        m.setMid(target.getMember());
        m.setId(target.getSessionId());
        return m;
    }

    @Override
    public Optional<MemberOnlineVo> transform(Optional<MemberOnline> entity) {
        return entity.map(ins->{
            return marshal(ins);
        });
    }
}